package com.jqb.sturts2lab02.action;

import com.jqb.struts2lab02.service.LoginService;
import com.jqb.struts2lab02.bean.User;
import com.jqb.struts2lab02.service.impl.LoginServiceImpl;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 注解需要的插件(struts2-convention-plugin.jar)
 * 可以通过注解方式来实现零配置
 * @ParentPackage("struts-default")
 * @Action(value="login",results={@Result(location="login.jsp"),@Result(name="input",location="login.jsp")})
 * @InterceptorRef(value="defaultStack")
 * 
 * */

public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private LoginService loginservice = new LoginServiceImpl();

	private String username;
	private String password;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String execute() throws Exception {
	
		if (loginservice.isLogin(username, password)) {
			
			User user=new User();
			user.setUsername(username);
			user.setPassword(password);
			
			ActionContext.getContext().put("msg", this.getText("welcome", new String[] { username }));
			ActionContext.getContext().getSession().put("userInfo", user);
			
			return SUCCESS;
		}
		
		ActionContext.getContext().put("msg", this.getText("loginfail"));
		
		return INPUT;
	}
}
